<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
public function index(){
    $Jarr=M("Plan")->field("in_plan.jid,num,src,jtitle")->where('status=1')->join("in_content on in_plan.jid=in_content.jid")->join("in_photos on in_plan.jid=in_photos.jid")->limit(3)->select();
    $this->Jarr=$Jarr;
	$this->display();
}
//产品详情
	public function goodshow($id)
	{
		 $user=$_SESSION['id'];
		if(!empty($user)){
			$U=M("User");
			// $userinfo=$U->where('id='.$user)->find();

			$userinfo=$U->where('id='.$user)->find();		   
			$this->userinfo=$userinfo;
		    $Qin=M("Qinqing");
			$Qarr=$Qin->where('uid='.$user)->select();
			$this->Qarr=$Qarr;     
		}
		
		$this->user=$user;
		$Arr=M("Plan")->field("gyue,lcheng,in_plan.jid,jtitle,tiao,zhai,yfjin,maxbao,maxjun,waitdate,content,src,typeid")->where('in_plan.jid='.$id)->join("in_content on in_plan.jid=in_content.jid")->join("in_photos on in_plan.jid=in_photos.jid")->find();
			$Tarr=M("Type")->field('tname')->where('tid='.$Arr['typeid'])->find();
			$other=M("Other")->where('id=1')->find();
			$this->gongyue=$other['gongyue'];
 			$this->liucheng=$other['liucheng'];
			$this->Arr=$Arr;
			$this->Tarr=$Tarr;
		    $this->display();
	}
	public function type($id)
	{
		$data['typeid']=$id;
		$data['status']=1;
		$db=M("Plan");
		$Parr=$db->field("in_plan.jid,num,src,jtitle,zhai")->where($data)->join("in_content on in_plan.jid=in_content.jid")->join("in_photos on in_plan.jid=in_photos.jid")->select();
		// echo  $db->getLastSql();
		$this->Parr=$Parr;
		$this->display();
	}
	public function none()
	{
		$this->display();
	}




	public function panduan()
	{
	  echo  $uid=$_SESSION['id'];
	   if(empty($uid)){
	   	    echo '1';//未登录
	   	    die;
	   }else{
		 $Pe=M("User");
		  $uinfo=$Pe->field("realname,userid")->where('id='.$uid)->find();   
			if (!$uinfo['realname']&&!$uinfo['userid']) {				    
				  echo "0";//未实名
					    die;

				}

	   }
  
	}
	//加入计划

	public function joinPlan()
	{

		if(IS_POST){
			    $user=$_SESSION['id'];
				$data['uid']=$user;
				$data['jid']=I("post.plan");
				$data['yufujin']=I("post.yufu");
				$data['qid']=I("post.qname");
				   
				//用户信息（申报人）
				if($data['qid']=="benren"){
					$data['qid']='0';
					$user=M("User")->field("realname,id")->where('id='.$user)->find();
					$data['username']=$user['realname'];    
					$this->user=$user;        
				}else{
					$wheres['qid']=$data['qid'];
					$Qin=M("Qinqing")->field("qname,qcard,twith")->where($wheres)->find();  
					$data['username']=$Qin['qname'];
					$this->qin=$Qin;   
				}
				//计划信息
				$Parr=M("Plan")->field("jtitle,src")->where('in_plan.jid='.$data['jid'])->join('in_photos on in_plan.jid=in_photos.jid')->find();
				$this->Parr=$Parr;
				//余额
				$YuMoney=M('Qianbao')->field('money')->where('uid='.$data['uid'])->find();
				$this->money=$YuMoney;	
				//剩余金额
				$shen=$YuMoney['money']-$data['yufujin'];
				$this->shen=$shen;	   
				$this->data=$data;
				$Bao=M("Baodan");
				$Barr=$Bao->where($data)->find();
				 $qian=M("Qianbao")->where('uid='.$data['uid'])->find();
			    if ($qian['zfpassword']=='') {
	              echo "<script>
	                alert('您未设置支付密码，设置后重试');
	                location.href='/Member/setPass';//客户端跳转
	                </script>";
			    }	
				if (!empty($Barr)) {
					    echo "<script>
					            alert('您或您的亲友已加入');
					           history.go(-1);
					          </script>";     
				}
				

					   	
			}  
				    
		$this->display();
		
	}
	public function pay()
	{
		if(IS_POST){
			//判断余额是否充足
			$shen=I("post.shen");
			if ($shen<0) {   
				    echo "<script>
				            alert('您的余额不足，请先充值');
				            history.go(-1);
				          </script>";
			}else{
			    $uid =I("post.user");
			    $data['jid'] =I("post.plan");
			    $data['yufujin'] =I("post.yf");
			    $data['qid'] =I("post.qin"); 
			    $data['username'] =I("post.uname"); 
			    $m['money'] = I("post.shen"); 
			    $password=I("post.p");
			    $pass=md5($password);
			    //用户钱包
			    // $qian=M("Qianbao")->where('uid='.$uid)->find();
			    // if ($qian['zfpassword']=='') {
			    // 	    echo "<script>
			    // 	            alert('您未设置支付密码，设置后重试');
			    // 	          </script>";
			    // }else{

              		 $zf=M("Qianbao")->field('zfpassword')->where('uid='.$uid)->find();   
              		    
			    	if ($pass==$zf['zfpassword']) {
			    	     $list=M("Qianbao")->where('uid='.$uid)->save($m);
				    	if ($list) {
				    		$j['uid']=$uid;
				    		$j['jine']=$data['yufujin'];
				    		$j['liuxiang']='8';
				    		$j['jiao_type']='0';
				    		$j['create_time']=date("Y:m:d H:i:s",time());
				    		$j['yue']=$m['money'];
				    		 $Ji=M("Jilu")->add($j);
				    		 if ($Ji) {
				    		 	$Plan=M('Plan');
				    		 	$Parr=$Plan->where('jid='.$data['jid'])->find();
				    		 	$newparr['allmoney']=$Parr['allmoney']+$data['yufujin'];
				    		 	$jihua=$Plan->where('jid='.$data['jid'])->save($newparr);
				    		 	$p['num']=$Parr['num']+1;
				    		 	$Plan->where('jid='.$data['jid'])->save($p);
				    		 	$usetime=$Parr['waitdate'];
				    		 	$usetime=strtotime("+ $usetime days",time());
				    		 	$B['usetime']=date("Y:m:d H:i:s",$usetime);
				    		 	$B['jid']=$data['jid'];
				    		 	$B['username']=$data['username'];
				    		 	$B['yufujin']=$data['yufujin'];
				    		 	$B['qid']=$data['qid'];
				    		 	$B['uid']=$uid;
				    		 	$B['bstatus']=0;
				    		 	$B['addtime']=date("Y:m:d H:i:s",time()); 
				    		 	$B['jname']=$Parr['jtitle'];
				    		 	$userid=M("User")->where("id=".$uid)->find();
				    		 	$B['userid']=$userid['userid'];
				    		 	$Bao=M("Baodan");
				    		 	$ba=$Bao->add($B);

				    		 	if ($ba&&$jihua) {
				    		 		    echo "<script>
				    		 		            alert('加入成功,请到 我-互保单 中查看详情');
				    		 		            history.go(-2);
				    		 		          </script>";
				    		 	}else{
				    		 		die('失败了！联系管理员');
				    		 	}
				    		 }
				    		}
			    	}else{

			    		    echo "<script>
			    		            alert('密码错误');
			    		            history.go(-1)
			    		          </script>";
			    		// die('密码错误');
			    	}
			    // }

  
			}
		}
	}
}